package com.inkyi.assistant.app.tools.comps;

import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.inkyi.assistant.app.tools.base.InkTools;

import javax.swing.*;
import java.awt.*;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class EnvironmentVarTool extends JPanel implements InkTools {

    public EnvironmentVarTool() {
        initComponents();
    }

    private JTabbedPane tabbedPane;

    private JToolBar trailing;

    private JButton refreshButton;

    private EnvironmentVarTable sysPanel;

    private EnvironmentVarTable javaPanel;

    @Override
    public void initComponents() {
        this.setLayout(new CardLayout());

        sysPanel = new EnvironmentVarTable();
        javaPanel = new EnvironmentVarTable();

        tabbedPane = new JTabbedPane();
        tabbedPane.addTab("系统变量", sysPanel);
        tabbedPane.addTab("Java变量", javaPanel);


        trailing = new JToolBar();
        trailing.setFloatable(false);
        trailing.setBorder(null);
        tabbedPane.putClientProperty(FlatClientProperties.TABBED_PANE_TRAILING_COMPONENT, trailing);

        refreshButton = new JButton(new FlatSVGIcon("icons/refresh.svg", 16, 16));
        refreshButton.addActionListener(e -> {
            refreshTableData();
        });
        trailing.add(refreshButton);

        refreshTableData();
        this.add(tabbedPane);
    }


    private void refreshTableData() {
        sysPanel.clearRow();
        javaPanel.clearRow();
        Map<String, String> env = System.getenv();
        List<Map.Entry<String, String>> envList = env.entrySet().stream().sorted(Comparator.comparing(Map.Entry<String, String>::getKey)).toList();
        for (Map.Entry<String, String> entry : envList) {
            sysPanel.addRow(new Object[]{entry.getKey(), entry.getValue()});
        }

        Properties properties = System.getProperties();
        List<Map.Entry<Object, Object>> propList = properties.entrySet().stream().sorted(Comparator.comparing(entry -> entry.getKey().toString())).toList();
        for (Map.Entry<Object, Object> entry : propList) {
            javaPanel.addRow(new Object[]{entry.getKey(), entry.getValue()});
        }
    }


    @Override
    public String getToolCode() {
        return "evn-var";
    }

    @Override
    public String getToolName() {
        return "环境变量";
    }

    @Override
    public String getToolID() {
        return "tools-" + getToolCode();
    }

    @Override
    public JComponent getComponent() {
        return this;
    }

    @Override
    public String getOpenType() {
        return "workspace";
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        EnvironmentVarTool panel = new EnvironmentVarTool();
        frame.setContentPane(panel);
        frame.setVisible(true);
    }


}
